package com.bawei.module_mine.register

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityRegisterBinding
import com.bawei.module_mine.viewmodel.RegisterViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = Router.REGISTER_PAGE)
class RegisterActivity : BaseActivity<RegisterViewModel,ActivityRegisterBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_register
    private val viewModel:RegisterViewModel by viewModels()
    private var code:Int=0
    private val djs:Int=60
    override fun initView() {
        super.initView()
        mBinding.toLogin.setOnClickListener {
            ARouter.getInstance().build(Router.LOGIN_PAGE).navigation()
        }

    }

    override fun initData() {
        super.initData()
        mBinding.getCode.setOnClickListener {
            if(mBinding.username==null){
                ToastUtils.showLong(this,"用户名不可为空")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                for (i in 10 downTo 0){
                    delay(1000)
                    mBinding.getCode.text="$i"
                    if(i==0){
                        mBinding.getCode.text="重新获取验证码"
                    }
                }
            }
            code = java.util.Random().nextInt(899999) + 100000
            mBinding.code.setText(code.toString() + "")
        }
        mBinding.btnRegister.setOnClickListener {
            if(!mBinding.code.text.toString().equals(code.toString())){
                ToastUtils.showLong(this,"验证码有误，请重新输入")
                return@setOnClickListener
            }
            if(!mBinding.password.text.toString().equals(mBinding.repassword.text.toString())){
                ToastUtils.showLong(this,"两次密码不一致，请重新输入")
                return@setOnClickListener
            }
            viewModel.register(mBinding.username.text.toString(),mBinding.password.text.toString())
        }
    }

    override fun initListener() {
        super.initListener()
        viewModel.noticeState.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed -> {
                    ToastUtils.showLong(this,"注册失败")
                }
                Loading -> {

                }
                Success -> {
                    ToastUtils.showLong(this,"注册成功")
                    ARouter.getInstance().build(Router.LOGIN_PAGE).navigation()
                }
            }
        }
    }

    override fun initStatusBar() {
        super.initStatusBar()
    }
}